package com.ztgf.sys.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.sys.models.bo.SysDictBO;
import com.ztgf.sys.models.bo.SysDictSaveBO;
import com.ztgf.sys.models.dto.SysDictDTO;
import com.ztgf.sys.models.service.ISysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 系统配置表控制器
 *
 * @author mxli
 * @Date 2020-05-27 00:03:38
 */

@RestController
@RequestMapping("/cms/sysDict")
@Slf4j
@Api(value = "SysDictController",tags = "系统配置表 接口")
public class SysDictController {

    @DubboReference
    private ISysDictService sysDictService;

    @ApiOperation(value = "添加系统配置表 ",notes = "添加系统配置表 ")
    @PostMapping(value = "/insert")
    public Result insert(SysDictSaveBO saveBO) {
        boolean flag = sysDictService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "修改系统配置表 ",notes = "修改系统配置表 ")
    @PostMapping("/update")
    public Result update(SysDictSaveBO saveBO){
        SysDictDTO dto = new SysDictDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = sysDictService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "删除系统配置表 ",notes = "删除系统配置表 ")
    @ApiImplicitParam(name = "id",value = "数据字典表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = sysDictService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除系统配置表 ",notes = "批量删除系统配置表 ")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        sysDictService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysDictDTO> get(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        SysDictDTO dto = sysDictService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "系统配置表 表列表",notes = "系统配置表 表列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysDictDTO>> queryPageList(SysDictBO bo){
        Wrapper<SysDictDTO> wrapper = new EntityWrapper<>();

        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());
        }
        if (StrUtils.isNotEmpty(bo.getName())){
            wrapper.eq("name", bo.getName());
        }
        Page<SysDictDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = sysDictService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @ApiOperation(value = "系统配置表 表列表",notes = "系统配置表 表列表")
    @PostMapping("/queryChildList")
    @ApiImplicitParam(name = "code",value = "关键字",required = true,paramType = "query", dataType = "String")
    public Result<List<SysDictDTO>> queryChildList(String code){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("value",code);
        SysDictDTO dto = sysDictService.selectOne(wrapper);
        if(dto == null){
            return new Result<>(new ArrayList<>());
        }
        wrapper = new EntityWrapper();
        wrapper.eq("parent_id", dto.getId());
        List<SysDictDTO> list = sysDictService.selectList(wrapper);
        return new Result<>(list);
    }

}
